/**
 * 匠言知识付费v2.0.0
 * Author: 山西匠言网络科技有限公司
 * 这不是一个免费软件，它受限于许可条款，你可以访问https://www.zsfzxkc.cn/获取更多详细信息。
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
<template>
	<view class="container" v-if="!nodata">
		<view class="teacher-course">
			<view class="teacher-course-list" v-for="(item, index) in subecribeList" :key="index">
				<view class="teacher-course-list-left">
					<text>{{item.menuname}}</text>
					<text>{{item.jianjie}}</text>
					<view class="teacher-keshi">
						<image src="../../../static/book.png" mode="aspectFit"></image>
						<text>{{item.ksnum}}课时</text>
						<text>￥{{item.price}}</text>
					</view>
					<view class="edit">
						<text @click="openEdit(item.id,item.menuname,item.price)">编辑</text>
						<text @click="shangjia(item.status,item.id)">{{item.status==1 ? '已上架' : '已下架'}}</text>
						<text @click="dianping(item.id)">点评作业</text>
					</view>
				</view>
				<image class="teacher-course-list-right" :src="item.thumb" mode="aspectFill"></image>
			</view>
		</view>
		
		
		<!-- 弹出层 -->
		<view class="tanchuceng" v-if="isEdit === true">
			<view class="popup">
				<view class="popup-top" >
					<text>课程名称</text>
					<input type="text" placeholder="请输入课程名称" @input='changeName' :value="menuname"/>
					<text>金额</text>
					<input type="text" placeholder="请输入金额" @input='changePrice' :value="price"/>
				</view>
				<view class="confirm">
					<text @click="closeEdit(false)">取消</text>
					<text @click="closeEdit(true)">确定</text>
				</view>
			</view>
		</view>
		
		
	</view>
	<view class="nodata" v-else>
		<image src="../../../static/wudingdan.png" mode="aspectFit"></image>
	</view>
</template>

<script>
	import {checkUserinfo} from '@/request/checkUserinfo'
	// import { postMyDingyue } from '@/request/courses'
	export default {
		data() {
			return {
				isEdit: false,
				index: 0,
				nodata:false,
				subecribeList:{},
				HOST_URL:uni.HOST_URL,
				menuname:'',
				price:'',
				menuid:'',
				tid:''
			};
		},
		
		onShareAppMessage(res) {
			let path = getCurrentPages()
			let path_share = path[0].$page.fullPath
			let path_title = path[0].data.title
			let userinfo = uni.getStorageSync('userinfo')
			let base_set = uni.getStorageSync('base_set')
			if(userinfo.uid=='' || !userinfo.uid){
				uni.navigateTo({
					url:'../login/login'
				})
				return {
					title: '请先登录后再分享给好友',
					path: ''
				}
			}else{
				if (res.from === 'button') {
					
				}
				return {
					title: base_set.title,
					path: `${path_share}?pid=${userinfo.uid}`
				}
			}
		},
		onLoad(e) {
			console.log(e)
			this.tid=e.tid
			this.getMycourse(e.tid)
		},
		onShow() {
			checkUserinfo()
			
		},
		methods: {
			dianping(id){
				uni.navigateTo({
					url:'../dianping-list/dianping-list?courseid='+id+'&tid='+this.tid
				})
			},
			getMycourse(tid){
				const BASE_URL = uni.BASE_URL
				uni.request({
					url: BASE_URL+'index/teacher/mycourse',
					method:'POST',
					data:{
						tid:tid,
					},
					success:(res) =>{
						console.log(res.data);
						if(res.data.code==0){
							this.subecribeList=res.data.data
						}else{
							this.nodata=true
						}
					},
					fail:(res)=> {
						console.log(res.data);
					}
				});
			},
			changeName(e){
				this.menuname =e.detail.value
			},
			changePrice(e){
				this.price =e.detail.value
			},
			openEdit(menuid,menuname,price) {
				this.menuid = menuid
				this.menuname = menuname
				this.price = price
				this.isEdit = true
			},
			closeEdit(e) {
				if(e) {
					const BASE_URL = uni.BASE_URL
					uni.request({
						url: BASE_URL+'index/teacher/course_edit',
						method:'POST',
						data:{
							id:this.menuid,
							menuname:this.menuname,
							price:this.price
						},
						success:(res) =>{
							console.log(res.data);
							if(res.data.code==0){
								uni.showToast({
									title:'修改成功'
								})
								this.isEdit = false
								this.getMycourse(this.tid)
							}
						},
						fail:(res)=> {
							console.log(res.data);
						}
					});
					
				} else {
					this.isEdit = false
				}
			},
			shangjia (e,id) {
				var data={}
				data.id=id
				if(e==1) {
					let content='确定要下架吗'
					uni.showModal({
						title: '提示',
						content: content,
						showCancel: true,
						cancelText: '取消',
						confirmText: '确定',
						success: res => {
							if(res.confirm) {
								data.status = e==1?0:1
								const BASE_URL = uni.BASE_URL
								uni.request({
									url: BASE_URL+'index/teacher/course_edit',
									method:'POST',
									data:data,
									success:(res1) =>{
										console.log(res1.data);
										if(res1.data.code==0){
											uni.showToast({
												title:'修改成功'
											})
											this.isEdit = false
										}
										this.getMycourse(this.tid)
									},
									fail:(res1)=> {
										console.log(res1.data);
									}
								});
							}
						}
					})
				} else {
					let content='确定要上架吗'
					uni.showModal({
						title: '提示',
						content: content,
						showCancel: true,
						cancelText: '取消',
						confirmText: '确定',
						success: res => {
							if(res.confirm) {
								data.status = e==1?0:1
								const BASE_URL = uni.BASE_URL
								uni.request({
									url: BASE_URL+'index/teacher/course_edit',
									method:'POST',
									data:data,
									success:(res1) =>{
										console.log(res1.data);
										if(res1.data.code==0){
											uni.showToast({
												title:'修改成功'
											})
											this.isEdit = false
										}
										this.getMycourse(this.tid)
									},
									fail:(res1)=> {
										console.log(res1.data);
									}
								});
							}
						}
					})
				}

			}
		}
	}
</script>

<style lang="less" scoped>

.teacher-course {
	padding: 0 20upx;
	&-list {
		padding: 20rpx;
		margin-top: 10upx;
		box-sizing: border-box;
		border-radius: 10upx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		&-left {
			width: 60%;
			display: flex;
			flex-direction: column;
			text{
				padding: 5rpx;
			}
			text:nth-child(1) {
				font-size: 28upx;
				font-weight: 700;
				color: #333;
			}
			text:nth-child(2) {
				font-size: 22upx;
				color: #555;
			}
			.teacher-keshi {
				display: flex;
				align-items: center;
				image {
					width: 30upx;
					height: 30upx;
				}
				text {
					font-size: 25upx;
					color: #555;
					margin-left: 10upx;
				}
				text:last-child {
					margin-left: 30upx;
				}
			}
			.edit {
				display: flex;
				border-top: 1upx solid #eee;
				padding: 10upx;
				align-items: center;
				justify-content: space-between;
				text {
					font-size: 25upx;
					color: #555;
					font-weight: 400;
					color: red;
					border: 1rpx solid red;
					border-radius: 8rpx;
					padding: 5rpx;
				}
			}
		}
		&-right {
			width: 250upx;
			height: 150upx;
			border-radius: 10rpx;
		}
	}
}


.tanchuceng {
	width: 100vh;
	height: 100vh;
	background: rgba(0,0,0,.4);
	position: fixed;
	top: 0;
	left: 0;
	.popup {
		width: 80%;
		height: 420upx;
		background-color: #fff;
		border-radius: 10upx;
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -60%);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		&-top {
			padding: 20upx 20upx 0;
			display: flex;
			flex-direction: column;
			text {
				font-size: 32upx;
				color: #333;
				margin-bottom: 15upx;
			}
			input {
				width: 100%;
				height: 65upx;
				border: 2upx solid #eee;
				margin-bottom: 15upx;
				border-radius: 5upx;
				padding: 0 20upx;
				box-sizing: border-box;
				color: #333;
			}
		}
		.confirm {
			display: flex;
			border-top: 2upx solid #eee;
			text {
				width: 50%;
				height: 100upx;
				// background-color: #007AFF;
				font-size: 35upx;
				font-weight: 700;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			text:last-child {
				color: #007AFF;
			}
		}
	}
}
.nodata{
	text-align: center;
}

</style>
